function ajaxSubmit(formId){
	$('input, option, checkbox, select, button, radio').removeClass("formError");
	$.ajax({
		url: $('#'+formId).attr('action'),
		type: 'POST',
		data: $('#'+formId).serializeArray(),
		success: ajaxReturn
	});
	return false;
}

function ajaxRemove(url,remove_message){
	if(window.confirm(remove_message)){
		$.ajax({
			url: url,
			type: 'GET',
			success: ajaxReturn
		});
	}
}

function ajaxReturn(jSonMsg){
	try{
		var response = eval('('+jSonMsg+')');
	} catch(e){}
			
	try{ 
		if(true == response.redirect){
			window.location.replace(response.url);
			return true;
		}
	} catch(e){}
			
	try{
		$.each(response.content, function(key,value){
			if("_external"==key){
				$.each(value,displayError);
			}else{
				displayError(key, value);
			}
		});
	}catch(e){}
}

function displayError(fieldId,message){
	alert(fieldId+' - '+message.toString());
	$('#'+fieldId).addClass("formError");
}

function loading(message){
	var header = $(document.createElement('div'));
	header.attr('class','popHeader');
	header.append(document.createTextNode(message));
	
	var img = $(document.createElement('img'));
	img.attr('alt','loading');
	img.attr('src','static/images/ajax-loader.gif');
	
	var pop = $(document.createElement('div'));
	pop.attr('id','pop');
	pop.append(header);
	pop.append(img);
	
	var fade = $(document.createElement('div'));
	fade.attr('id','fade');
	fade.attr('class','overlay');
	
	$(document.body).append(pop);
	$(document.body).append(fade);
	$(".overlay").css({"width":$(document).width(),"height":$(document).height()});
}

function removeLoading(){
	$('#pop').remove();
	$('#fade').remove();
}

function file_uploaded(field,file){
	$('#'+field).val(file);
	try{
		$('#img_'+field).attr("src", URL_ROOT+file+'?'+Math.random());
	}catch(e){alert("Erro!");}
}